package com.text4;

import java.sql.*;


public class Cha {
    public static void main(String[] args) throws SQLException {
        Cha cha = new Cha();
        cha.selectById(6,"12","3", "4",5);
    }
    public void selectById(int id,String userName ,String password, String email,int userType) throws SQLException {
        Connection connection=null;
        Statement statement=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/kgcnews";
            String username="root";
            String password1="123456";
            connection= DriverManager.getConnection(url,username,password1);
            String sql="SELECT * FROM news_user WHERE id=? and userName=? and password=? and email=? and userType=?";
            //statement=connection.createStatement();//不带条件的查询
            //带条件的查询
            preparedStatement= connection.prepareStatement(sql);

            preparedStatement.setInt(1,id);
            preparedStatement.setString(2,userName);
            preparedStatement.setString(3,password);
            preparedStatement.setString(4,email);
            preparedStatement.setInt(5,userType);


            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                int ids=resultSet.getInt("id");
                int name1=resultSet.getInt("userName");
                String date1 = resultSet.getString("password");
                String date2 = resultSet.getString("email");
                String date3 = resultSet.getString("userType");

                System.out.println(ids+"-----"+name1 + "------" + date1+ "------" + date2+ "------" + date3);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            resultSet.close();
            preparedStatement.close();
            connection.close();
        }
    }
}
